<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 菜单管理
 * @exception 400 参数错误
 * @exception 502 服务器错误
 * @exception 503 数据库错误
 */
class MenuController extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Request');
        $this->load->model('Menu');
    }


    /**
     * @return array
     */
    static function getRules()
    {
        return [
            'menuTree' => [
                'pid' => [
                    'name' => 'pid', 'type' => 'int', 'require' => false, 'default' => 0, 'desc' => '父级id', 'source' => 'get'
                ]
            ],
            'addMenu' => [
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 20, 'require' => true, 'desc' => '名称(20字符内)', 'source' => 'post'
                ],
                'index' => [
                    'name' => 'index', 'type' => 'string', 'max' => 45, 'require' => true, 'desc' => '跳转链接(45字符内)', 'source' => 'post'
                ],
                'icon' => [
                    'name' => 'icon', 'type' => 'string', 'max' => 45, 'require' => false,'default' => '', 'desc' => '菜单图标样式类', 'source' => 'post'
                ],
                'parentId' => [
                    'name' => 'parentId', 'type' => 'int', 'require' => false, 'default' => 0, 'desc' => '父级id(没有则不传或传0)', 'source' => 'post'
                ],
                'img' => [
                    'name' => 'img', 'type' => 'string', 'require' => true, 'default' => '', 'desc' => '小图片', 'source' => 'post'
                ],
                'icon' => [
                    'name' => 'icon', 'type' => 'string', 'require' => true, 'default' => '', 'desc' => '图标', 'source' => 'post'
                ],
                'sort' => [
                    'name' => 'sort', 'type' => 'int', 'require' => false, 'default' => 0, 'desc' => '排序值(越小越靠前)', 'source' => 'post'
                ]
            ],
            'modMenu' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '菜单id', 'source' => 'post'
                ],
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 20, 'require' => false, 'desc' => '名称(20字符内)', 'source' => 'post'
                ],
                'index' => [
                    'name' => 'index', 'type' => 'string', 'max' => 45, 'require' => false, 'desc' => '跳转链接(45字符内)', 'source' => 'post'
                ],
                'icon' => [
                    'name' => 'icon', 'type' => 'string', 'max' => 45, 'require' => false, 'desc' => '菜单图标样式类', 'source' => 'post'
                ],
                'sort' => [
                    'name' => 'sort', 'type' => 'int', 'require' => false, 'desc' => '排序值(越小越靠前)', 'source' => 'post'
                ],
                  'img' => [
                    'name' => 'img', 'type' => 'string', 'require' => false, 'desc' => '小图片', 'source' => 'post'
                ],
            ],
            'delMenu' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '菜单id', 'source' => 'post'
                ],
            ]
        ];
    }

    /**
     * 菜单结构
     * @desc 获取组织结构树(启用的)
     */
    public function menuTree()
    {
        $result = $this->db->where('disabled',0)
        ->get('menu')->result_array();
        restful(200, $result);
    }


    /**
     * 菜单
     * @desc 添加
     * @throws MyException
     */
    public function addMenu()
    {
        $data = $this->request->getData();

        $result = $this->menu->add($data);
        if ($result !== false) {
            restful(201, '操作成功');
        }
        throw new MyException('', MyException::DATABASE);
    }


    /**
     * 菜单
     * @desc 修改（名称、标识）
     * @throws MyException
     */
    public function modMenu()
    {
        $data = $this->request->getData();
        $result = $this->menu->mod($data);

        if ($result !== false) {
            restful(200, '操作成功');
        }

        throw new MyException('', MyException::DATABASE);
    }


    /**
     * 菜单
     * @desc 删除
     * @throws MyException
     */
    public function delMenu(int $id=0)
    {
        //获取当前菜单,如果不为空，则不能删除
        $has=$this->db->where('parentId',$id)->count_all_results('menu');
        if($has){
            restful(409, '菜单包含子菜单，不能删除');
        }

        $result = $this->menu->delete($id);
        if ($result !== false) {
            restful();
        }
        throw new MyException('', MyException::DATABASE);
    }

}
